package com.wll.store.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wll.store.user.dto.*;
import com.wll.store.user.entity.Address;
import com.wll.store.user.entity.User;
import com.wll.store.user.mapper.UserMapper;
import com.wll.store.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserInfoByNameAndPwd(UserInfo userInfo) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username",userInfo.getUsername()).eq("password",userInfo.getPassword());
        return this.getOne(qw);
    }

    @Override
    public boolean addUserInfo(UserInfo userInfo) {
        User user = new User();
        user.setUsername(userInfo.getUsername());
        user.setPassword(userInfo.getPassword());
        return this.save(user);
    }

    @Override
    public boolean addRoleWithAdmin(Integer userId) {
        //默认给用户添加admin角色
        boolean result = userMapper.addRoleWithAdmin(userId);
        return result;
    }


    @Override
    public User getUserInfoByName(String username) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username",username);
        return this.getOne(qw);
    }

    /*@Override
    public Page<User> getUserPage(UserPageInfo userPageInfo) {
        Page<User> page = new Page<>();
        //每页多少条数据
        page.setSize(userPageInfo.getPageSize());
        //第几页
        page.setCurrent(userPageInfo.getPageNumber());

        return this.page(page);
    }*/

    @Override
    public UserRole getUserRoleByUserId(Integer userId) {
        return userMapper.selectUserRoleByUserId(userId);
    }

    /**
     * 根据用户名修改用户电话和email
     * @param userinfo
     * @return
     */
    @Override
    public boolean updateUserInfo(User userinfo) {
        User user = new User();
        user.setUsername(userinfo.getUsername());
        user.setPhone(userinfo.getPhone());
        user.setEmail(userinfo.getEmail());
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("username",userinfo.getUsername());

        return this.update(user,wrapper);
    }

    /**
     * 根据用户旧密码修改密码
     * @param userPassword
     * @return
     */
    @Override
    public boolean updateUserPassword(UserPassword userPassword) {
        User user = new User();
        user.setPassword(userPassword.getNewPassword());
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("password",userPassword.getOldPassword());

        return this.update(user,wrapper);
    }


}
